package com.utils;

import org.springframework.util.ObjectUtils;
import org.testng.collections.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * 构建tree
 * @author 池豪杰
 * @date 2019年6月3日
 *
 */
public class BuildTree {

    /**
     * 构建完整树
     * @param nodes
     * @return
     */
    public static <T> Tree<T>  build(List<Tree<T>> nodes) {
        if(nodes == null){
            return null;
        }
        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            if (pid == null || "".equals(pid)) {
                topNodes.add(children);
                continue;
            }else if(pid.equals("-1")) {
                topNodes.add(children);
                continue;
            }
            //
            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildrenNode().add(children);
                    children.setChildren(true);
                    parent.setParent(true);
                    continue;
                }
            }
        }
       /* for (Tree<T> topNode :topNodes) {
            List<Tree<T>> childrenNode1 = topNode.getChildrenNode();
            if(!ObjectUtils.isEmpty(childrenNode1)){
                //第三级节点
                for (Tree<T> childrenNode : childrenNode1) {
                    //遍历所有其他
                    for (Tree<T> normalNode : nodes) {
                        //
                        String parentId = normalNode.getParentId();
                        if (parentId != null && parentId.equals(childrenNode.getId())) {
                            childrenNode.getChildrenNode().add(normalNode);
                            continue;
                        }
                    }
                }
            }

        }*/

        Tree<T> root = new Tree<T>();
        root.setId("-1");
        root.setParent(true);
        root.setChildren(false);
        root.setChecked(true);
        root.setChildrenNode(topNodes);
        root.setText("root");
        return root;
    }

    /**
     * 构建topNode节点及子节点
     * @param nodes
     * @return
     */
    public static <T> Tree<T> build(List<Tree<T>> nodes,Tree<T> topNode) {
        if(nodes == null){
            return null;
        }
        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildrenNode().add(children);
                    children.setChildren(true);
                    parent.setParent(true);
                    continue;
                }
            }
        }
        topNode.setChildrenNode(nodes);
        return topNode;
    }

    public static <T> List<Tree<T>> buildTag(List<Tree<T>> nodes) {
        if(nodes == null){
            return null;
        }
        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildrenNode().add(children);
                    children.setChildren(true);
                    parent.setParent(true);
                    continue;
                }
            }
        }
        return nodes;
    }


    public static <T> List<Tree<T>> buildMineTreeDG(List<Tree<T>> nodes) {
        List<Tree<T>> tagTree = buildTag(nodes);
        List<Tree<T>> newTree=Lists.newArrayList();
        //遍历找出叶子节点
        for (Tree<T> currentNode : nodes) {
            if(currentNode.isParent()){
                //移除并返回新集合
                buildMineTreeDG(currentNode.getChildrenNode());
            }else {
                newTree.add(currentNode);
                return newTree;
            }
        }
        return newTree;
    }

    /**
     * 找出顶级节点
     */
    public static <T> List<Tree<T>>  getTopNodes(List<Tree<T>> nodes) {
        if(nodes == null){
            return null;
        }
        List<Tree<T>>  topNodes=Lists.newArrayList();
        for (Tree<T> currentNode : nodes) {
             if(currentNode.getLevel().equals("1")){
                 topNodes.add(currentNode);
             }
        }
        //为topNode节点寻找子节点
        for (Tree<T> topNode:topNodes) {
            for (Tree<T> children : nodes) {
                String parentId = children.getParentId();
                if (parentId != null && parentId.equals(topNode.getId())) {
                      topNode.getChildrenNode().add(children);
                    continue;
                }
            }
        }
        //为顶节点的每一个孩子节点寻找子节点
        for (Tree<T> topNode:topNodes) {
            //顶节点的孩子节点
            List<Tree<T>> childrenNodes = topNode.getChildrenNode();
            if(!ObjectUtils.isEmpty(childrenNodes)){
                for (Tree<T> childrenNode : childrenNodes) {
                    //遍历所有其他
                    for (Tree<T> normalNode : nodes) {
                        //
                        String parentId = normalNode.getParentId();
                        if (parentId != null && parentId.equals(childrenNode.getId())) {
                            childrenNode.getChildrenNode().add(normalNode);
                            continue;
                        }
                    }
                }
            }

        }
        return  topNodes;
    }

    static <T> Tree<T>   judgeLeave(Tree<T> node,List<Tree<T>> nodes){
        boolean isLeave=false;
        for (Tree<T> next : nodes) {
            if(node.getId().equals(next.getParentId()))
                isLeave=true;
            break;
        }
        if(isLeave){
            return node;
        }else{
            return null;
        }
    }

    /**
     * 构建节点 返回集合
     * @param nodes
     * @return
     */
    public static <T> List<Tree<T>> buildChidren(List<Tree<T>> nodes) {
        if(nodes == null){
            return null;
        }
        for (Tree<T> children : nodes) {
            String pid = children.getParentId();
            for (Tree<T> parent : nodes) {
                String id = parent.getId();
                if (id != null && id.equals(pid)) {
                    parent.getChildrenNode().add(children);
                    children.setParent(true);
                    parent.setChildren(true);
                    continue;
                }
            }
        }
        return nodes;
    }

    /**
     * 构建完整树
     * @param nodes
     * @return
     */
    public static <T> SimpleTree<T> buildSimple(List<SimpleTree<T>> nodes) {
        if(nodes == null){
            return null;
        }
        List<SimpleTree<T>> topNodes = new ArrayList<SimpleTree<T>>();
        for (SimpleTree<T> children : nodes) {
            String pid = children.getP();
            if (pid == null || "".equals(pid)) {
                topNodes.add(children);
                continue;
            }else if(pid.equals("-1")) {
                topNodes.add(children);
                continue;
            }
            for (SimpleTree<T> parent : nodes) {
                String id = parent.getI();
                if (id != null && id.equals(pid)) {
                    parent.getC().add(children);
                    continue;
                }
            }
        }
        SimpleTree<T> root = new SimpleTree<T>();
        root.setI("-1");
        root.setC(topNodes);
        root.setT("root");
        return root;
    }

}